package com.situ.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.PageInfo;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.service.StudentService;

@WebServlet("/student")
public class StudentController extends HttpServlet{
	private StudentService studentService = new StudentService();

	// 访问这个servlet时候默认会访问这个service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		
		// 先判断session里面有没有user这个对象
//		HttpSession session = req.getSession();
//		User user = (User)session.getAttribute("user");
//		if (user == null) {
//			// 用户根本没有登录,重定向到登录界面
//			resp.sendRedirect(req.getContextPath() + "/user_login.jsp");
//			return;
//		}
		
		// http://localhost:8080/Java1912WebStudy/student?method=findAll
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "findAllByPage";
		}
		
		switch (method) {
		case "findAll":
			findAll(req, resp);
			break;
		case "findByPage":
			findByPage(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "getStudentUpdatePage":
			getStudentUpdatePage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}

	

	private void update(HttpServletRequest req, HttpServletResponse resp) {
		
	}

	private void getStudentUpdatePage(HttpServletRequest req, HttpServletResponse resp) {
		
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
		
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		boolean isSuccess = studentService.insert(student);
		//resp.sendRedirect(req.getContextPath() + "/student?method=findAll");
		resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	}

	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentController.findAll()");
		List<Student> list = studentService.findAll();
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_list3.jsp").forward(req, resp);
	}
	
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// http://localhost:8080/Java1912WebStudy/student?method=findByPage&pageNo=2&pageSize=5
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		PageInfo pageInfo = studentService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		// 分页想要的所有数据都封装到PageInfo里面
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
}
